package cn.xsw.interceptor;

import cn.hutool.core.util.StrUtil;
import cn.xsw.common.CommonEnum;
import cn.xsw.common.JwtTokenUtil;
import cn.xsw.common.MyException;
import cn.xsw.entity.User;
import cn.xsw.service.impl.UserServiceImpl;
import io.jsonwebtoken.Claims;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthInterceptor implements HandlerInterceptor {

    @Resource
    private UserServiceImpl userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("S-TOKEN");
        if(StrUtil.hasEmpty(token))  throw new MyException(-1,"管理员未登录，请先登录！");
        Claims claims = JwtTokenUtil.verifyJwt(token);
        if(claims==null) throw new MyException(CommonEnum.UNAUTHORIZED);
        return  true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
